<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="股票名称" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入股票名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发行人分红状态" prop="feedbackDividend">
        <el-select v-model="queryParams.feedbackDividend" placeholder="请选择发行人分红状态" clearable size="small">
          <el-option
            v-for="dict in feedbackDividendOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="feedbackVerify">
        <el-select v-model="queryParams.feedbackVerify" placeholder="请选择审核状态" clearable size="small">
          <el-option
            v-for="dict in feedbackVerifyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评价编号" align="center" prop="id" width="150"/>
      <el-table-column label="股票名称" align="center" prop="stockName" width="280"/>
      <el-table-column label="发行人分红状态" align="center" prop="feedbackDividend" :formatter="feedbackDividendFormat" width="250"/>
      <el-table-column label="发行人资金使用情况" align="center" prop="feedbackMoneytrack" :formatter="feedbackMoneytrackFormat" width="250"/>
      <el-table-column label="发行人反馈评价" align="center" prop="feedbackEvaluate" :formatter="feedbackEvaluateFormat" width="150"/>
      <el-table-column label="审核状态" align="center" prop="feedbackVerify" :formatter="feedbackVerifyFormat" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.feedbackVerify === 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改阶段性反馈对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="我的股票" prop="name">
          <el-select v-model="department" placeholder="请选择我的股票" clearable size="small" @change="changeTheStock(department)">
            <el-option
              v-for="item in stockList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发行人分红状态">
          <el-radio-group v-model="form.feedbackDividend">
            <el-radio
              v-for="dict in feedbackDividendOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发行人资金使用情况">
          <el-radio-group v-model="form.feedbackMoneytrack">
            <el-radio
              v-for="dict in feedbackMoneytrackOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发行人反馈评价">
          <el-radio-group v-model="form.feedbackEvaluate">
            <el-radio
              v-for="dict in feedbackEvaluateOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="详细评价" prop="feedbackMessage">
          <el-input v-model="form.feedbackMessage" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
        <el-button @click="submitForm">保 存</el-button>
        <el-button type="primary" @click="submit">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback, exportFeedback } from "@/api/invest/asset/feedback";
import { selectHoldStockList } from "@/api/operate/stock";

export default {
  name: "Feedback",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 阶段性反馈表格数据
      feedbackList: [],
      //股票名单
      stockList:[],
      department: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 发行人分红状态字典
      feedbackDividendOptions: [],
      // 发行人资金使用情况字典
      feedbackMoneytrackOptions: [],
      // 发行人反馈评价字典
      feedbackEvaluateOptions: [],
      // 审核状态字典
      feedbackVerifyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockCode: null,
        stockName: null,
        stockOwnerid: null,
        stockOwnername: null,
        feedbackDividend: null,
        feedbackMoneytrack: null,
        feedbackEvaluate: null,
        feedbackMessage: null,
        feedbackVerify: null,
        deptId: null,
        createById: null,
        updateById: null,
        name:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        feedbackDividend: [
          { required: true, message: "发行人分红状态不能为空", trigger: "change" }
        ],
        feedbackMoneytrack: [
          { required: true, message: "发行人资金使用情况不能为空", trigger: "change" }
        ],
        feedbackEvaluate: [
          { required: true, message: "发行人反馈评价不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("feedback_dividend").then(response => {
      this.feedbackDividendOptions = response.data;
    });
    this.getDicts("feedback_moneytrack").then(response => {
      this.feedbackMoneytrackOptions = response.data;
    });
    this.getDicts("feedback_evaluate").then(response => {
      this.feedbackEvaluateOptions = response.data;
    });
    this.getDicts("feedback_verify").then(response => {
      this.feedbackVerifyOptions = response.data;
    });
  },

  methods: {
    /** 查询阶段性反馈列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
      });
      selectHoldStockList().then(response => {
        this.stockList = response.rows;
        this.loading = false;
      });

    },
    // 发行人分红状态字典翻译
    feedbackDividendFormat(row, column) {
      return this.selectDictLabel(this.feedbackDividendOptions, row.feedbackDividend);
    },
    // 发行人资金使用情况字典翻译
    feedbackMoneytrackFormat(row, column) {
      return this.selectDictLabel(this.feedbackMoneytrackOptions, row.feedbackMoneytrack);
    },
    // 发行人反馈评价字典翻译
    feedbackEvaluateFormat(row, column) {
      return this.selectDictLabel(this.feedbackEvaluateOptions, row.feedbackEvaluate);
    },
    // 审核状态字典翻译
    feedbackVerifyFormat(row, column) {
      return this.selectDictLabel(this.feedbackVerifyOptions, row.feedbackVerify);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockCode: null,
        stockName: null,
        stockOwnerid: null,
        stockOwnername: null,
        feedbackDividend: null,
        feedbackMoneytrack: null,
        feedbackEvaluate: null,
        feedbackMessage: null,
        feedbackVerify: null,
        deptId: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null,
        remark: null,
        name:null
      };
      this.department = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.feedbackVerify = "0";
      this.open = true;
      this.title = "添加阶段性反馈";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFeedback(id).then(response => {
        this.form = response.data;
        this.department = this.form.stockCode;
        this.open = true;
        this.title = "修改阶段性反馈";
      });
    },
    // 根据股票名查股票代码
    changeTheStock(item) {
      if (item) {
        this.form.stockCode = item;
        let stocks = this.stockList.filter(function (value, index, array) {
          return value.code === item;
        });
        if (stocks.length > 0) {
          this.form.stockName = stocks[0].name;
        }
      }
    },
    //提交进行审核
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.feedbackVerify = "1";
          if (this.form.id != null) {
            updateFeedback(this.form).then(response => {
              this.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeedback(this.form).then(response => {
              this.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          }

        }
      });
    },
    /** 保存按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFeedback(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeedback(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除阶段性反馈编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFeedback(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
  }
};
</script>
